package com.lixiaoyi.ems.controller;

import com.lixiaoyi.ems.entity.EmpManager;
import com.lixiaoyi.ems.service.IEmpManagerService;
import com.lixiaoyi.ems.service.impl.EmpManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author 李小毅
 * @Description
 * @Date 2022-05-18
 */

@WebServlet("/manager/empManagerLogin")
public class EmpManagerLoginController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.收参
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String inputVcode = request.getParameter("inputVcode");

        //2.验证码判断
        String codes = (String) request.getSession().getAttribute("codes");
        if (inputVcode != null && inputVcode.equalsIgnoreCase(codes)){

            //3.调用service层 login方法
            IEmpManagerService empManagerService = new EmpManagerServiceImpl();
            EmpManager empManager = empManagerService.login(username,password);
            //4.判空
            if (empManager != null){
                //不空 则存储在session中  用于后面权限验证
                HttpSession session = request.getSession();
                session.setAttribute("empManager",empManager);
                //重定向 员工信息展示页面
                response.sendRedirect(request.getContextPath() + "/manager/safe/showAllEmp");
            }else {
                //空 返回login页面重新登陆
                response.sendRedirect(request.getContextPath() + "/login.jsp");
            }
        }else {
            //空 返回login页面重新登陆
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        }



    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
